package com.zyz.bookborrowing.servlet;

import com.zyz.bookborrowing.bean.Classes;
import com.zyz.bookborrowing.bean.Department;
import com.zyz.bookborrowing.bean.User;
import com.zyz.bookborrowing.service.ClassServiceImpl;
import com.zyz.bookborrowing.service.DepartmentServiceImpl;
import com.zyz.bookborrowing.service.IClassService;
import com.zyz.bookborrowing.service.IDepartmentService;
import com.zyz.bookborrowing.utils.Constant;
import com.zyz.bookborrowing.utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.function.DoubleToIntFunction;

@WebServlet(name = "departmentServlet", urlPatterns = "/departmentServlet")
public class DepartmentServlet extends HttpServlet {

    IDepartmentService departmentService = new DepartmentServiceImpl();
    IClassService classService = new ClassServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1. 获取请求的类型
        String type = req.getParameter(Constant.REQUEST_PARAMETER_TYPE);
//        2. 如果type不为空，那么根据type不同值做出不同操作
        if (StringUtils.isNotEmpty(type)){
            if (Constant.SERVLET_TYPE_QUERY.equals(type)){
                queryList(req, resp);
            } else if (Constant.SERVLET_TYPE_SAVE.equals(type)){
                saveDepartment(req, resp);
            } else if (Constant.SERVLET_TYPE_UPDATE.equals(type)) {
                updateDepartment(req, resp);
            } else if (Constant.SERVLET_TYPE_DELETE.equals(type)) {
                deleteDepartment(req, resp);
            } else if (Constant.SERVLET_TYPE_QUERYBYID.equals(type)) {
                String id = req.getParameter("id");
//                System.out.println(id);
                Department department = departmentService.queryById(Integer.parseInt(id));
//                System.out.println(department);
                req.setAttribute("dept", department);
                req.getRequestDispatcher("/depart/departmentUpdate.jsp").forward(req, resp);
            } else {
                queryList(req, resp);
            }
        } else {
            queryList(req, resp);
        }
    }

    private void deleteDepartment(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
//        删除院系前，判断班级存在否（此方法可以借用下拉列表功能时，获取院系下班级的方法）
        List<Classes> classes = classService.queryClsByDepartId(Integer.parseInt(id));
        if (classes != null && classes.size() > 0){
            resp.setContentType("text/html;charset=UTF-8");
            PrintWriter writer = resp.getWriter();
            writer.println("<script type='text/javascript' >");
            writer.println("alert('当前院系还有班级存在，不能删除');");
            writer.println("location.href='/departmentServlet?type=query';");
            writer.println("</script>");
            writer.flush();
        } else {
            departmentService.deleteById(Integer.parseInt(id));
            resp.sendRedirect("/departmentServlet?type=query");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    private void queryList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Department> list = departmentService.list(null);
        req.setAttribute("list", list);
        req.getRequestDispatcher("/depart/department.jsp").forward(req, resp);
    }

    private void saveDepartment(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Department dept = new Department();
        dept.setDepartment(req.getParameter("department"));
        dept.setDeptDesc(req.getParameter("deptDesc"));
        System.out.println(dept.getId());
        Integer count = -1;
//                count:影响的行数
        count = departmentService.saveDepartment(dept);
        if (count > 0){
//                    表示增/更成功,再做一次查询操作
            resp.sendRedirect("/departmentServlet?type=query");
        } else {
//                    插入失败
            System.out.println("插入失败...");
//                    跳转失败页面
        }
    }
    private void updateDepartment(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Department dept = new Department();
        String id = req.getParameter("id");
        dept.setId(Integer.parseInt(id));
        dept.setDepartment(req.getParameter("department"));
        dept.setDeptDesc(req.getParameter("deptDesc"));
        System.out.println(dept.getId());
        Integer count = -1;
//        System.out.println(dept);
//        System.out.println("更新");
//            更新
        count = departmentService.updateDepartment(dept);
        if (count > 0){
//                    表示增/更成功,再做一次查询操作
            resp.sendRedirect("/departmentServlet?type=query");
        } else {
//                    插入失败
            System.out.println("插入失败...");
//                    跳转失败页面
        }
    }

}
